package cn.lingyangwl.agile.system.controller;

import cn.lingyangwl.agile.system.model.rqrs.accesskey.AccessKeyQuery;
import cn.lingyangwl.agile.system.model.rqrs.accesskey.AccessKeyResp;
import cn.lingyangwl.agile.system.model.rqrs.accesskey.AccessKeySaveReq;
import cn.lingyangwl.framework.core.response.Resp;
import cn.lingyangwl.framework.validation.group.UpdateGroup;
import cn.lingyangwl.agile.system.model.rqrs.accesskey.AccessKeyDetailsReq;
import cn.lingyangwl.agile.system.service.AccessKeyService;
import cn.lingyangwl.agile.common.security.annotation.InnerAuth;
import cn.lingyangwl.agile.common.security.utils.SecurityUtils;
import cn.lingyangwl.agile.ram.ActionType;
import cn.lingyangwl.agile.ram.RamResource;
import io.swagger.annotations.Api;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * API访问密钥
 *
 * @author shenguangyang
 */
@Api(tags = "API访问密钥")
@RestController
@RequestMapping("/accessKey")
public class AccessKeyController {
    @Resource
    private AccessKeyService accessKeyService;

    /**
     * API访问密钥
     */
    @GetMapping("/listAccessKey")
    @RamResource(action = "sys:accessKey:query", name = "查询访问密钥", type = ActionType.READ)
    public Resp<List<AccessKeyResp>> listAccessKey(@Validated AccessKeyQuery query) {
        return Resp.ok(accessKeyService.listAccessKey(query));
    }

    /**
     * 通过id查找 API访问密钥, 只能内网访问(查询的详细数据)
     */
    @InnerAuth
    @GetMapping("/getAccessKeyById")
    @RamResource(action = "sys:accessKey:query", name = "查询访问密钥", type = ActionType.READ)
    public Resp<AccessKeyResp> getAccessKeyById(@RequestParam(value = "id") Long id) {
        return Resp.ok(accessKeyService.getAccessKeyById(id, SecurityUtils.getLoginUser()));
    }

    /**
     * 获取基础信息
     */
    @GetMapping("/getAccessKeyBaseInfoById")
    @RamResource(action = "sys:accessKey:query", name = "查询访问密钥", type = ActionType.READ)
    public Resp<AccessKeyResp> getAccessKeyBaseInfoById(@RequestParam(value = "id", required = false) Long id) {
        return Resp.ok(accessKeyService.getAccessKeyBaseInfoById(id));
    }

    /**
     * 获取密钥详情, 供外部调用, 需要做安全校验
     */
    @GetMapping("/getAccessKeyDetails")
    @RamResource(action = "sys:accessKey:query", name = "查询访问密钥", type = ActionType.READ)
    public Resp<AccessKeyResp> getAccessKeyDetails(@Validated AccessKeyDetailsReq req) {
        return Resp.ok(accessKeyService.getAccessKeyDetails(req));
    }

    /**
     * 通过id查找 API访问密钥
     */
    @InnerAuth
    @GetMapping("/getAccessKeyBySecretId")
    @RamResource(action = "sys:accessKey:query", name = "查询访问密钥", type = ActionType.READ)
    public Resp<AccessKeyResp> getAccessKeyBySecretId(@RequestParam(value = "secretId") String secretId) {
        return Resp.ok(accessKeyService.getAccessKeyBySecretId(secretId));
    }

    /**
     * 保存 API访问密钥
     */
    @PostMapping("/saveAccessKey")
    @RamResource(action = "sys:accessKey:save", name = "保存访问密钥", type = ActionType.WRITE)
    public Resp<?> saveAccessKey(@RequestBody AccessKeySaveReq rq) {
        return Resp.ok(accessKeyService.saveAccessKey(rq));
    }

    /**
     * 更新 API访问密钥
     */
    @PostMapping("/updateAccessKeyById")
    @RamResource(action = "sys:accessKey:update", name = "更新访问密钥", type = ActionType.WRITE)
    public Resp<?> updateAccessKeyById(@Validated(UpdateGroup.class) @RequestBody AccessKeySaveReq rq) {
        return Resp.ok(accessKeyService.updateAccessKeyById(rq));
    }

    /**
     * 批量删除 API访问密钥
     */
    @DeleteMapping("/deleteAccessKeyByIds")
    @RamResource(action = "sys:accessKey:delete", name = "删除访问密钥", type = ActionType.WRITE)
    public Resp<?> deleteBatch(@RequestParam(value = "ids") List<Long> ids) {
        return Resp.ok(accessKeyService.batchDeleteAccessKey(ids));
    }

    /**
     * 删除 API访问密钥
     */
    @DeleteMapping("/deleteAccessKeyById")
    @RamResource(action = "sys:accessKey:delete", name = "删除访问密钥", type = ActionType.WRITE)
    public Resp<?> delete(@RequestParam(value = "id") Long id) {
        return Resp.ok(accessKeyService.deleteAccessKey(id));
    }
}
